package com.gxa.controller.basic_commodity_settings;

import com.gxa.common.Constants;
import com.gxa.common.PaginationResult;
import com.gxa.common.ResponseData;
import com.gxa.pojo.*;
import com.gxa.service.ProductCodeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: zxy
 * Date: 2020/10/28 12:47
 * Description:
 * Version: V1.0
 */
@RestController
@RequestMapping("productcode")
@Api(value = "新增产品编码",tags = "新增产品编码")
public class ProductCodeController {

    @Autowired
    private ProductCodeService productCodeService;

    @Autowired
    private ResponseData responseData;

    @GetMapping(value = "/queryProCodeSelect")
    @ApiOperation(value = "查询产品编码下拉框数据",notes = "根据页面返回的值查询对应的产品编码下拉框数据",httpMethod = "Get", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResponseData queryProCodeSelect(){
        List<List<String>> proCode = productCodeService.queryProCodeSelect();
        if(proCode != null){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("查询成功");
            responseData.setData(proCode);
        }
        return responseData;
    }

    @PostMapping(value = "/delProCode")
    @ApiOperation(value = "删除产品编码",notes = "根据页面返回的值删除对应的产品编码信息",httpMethod = "Post", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="删除成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResponseData delProCode(@RequestBody @ApiParam(name = "billCodeId",value = "产品编码id") BillCode billCode){
        int result = productCodeService.delProCode(billCode.getBillCodeId());
        if(result > 0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("删除成功");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.NOTFOUND);
            responseData.setMsg("删除失败");
            responseData.setData(null);
        }
        return responseData;
    }

    @PostMapping(value = "/addProCode")
    @ApiOperation(value = "添加产品编码",notes = "根据页面返回的值添加对应的产品编码信息",httpMethod = "Post", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="添加成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResponseData addProCode(@RequestBody @ApiParam(name = "billCode",value = "产品编码") BillCode billCode){
        int result = productCodeService.addProCode(billCode);
        if(result > 0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("添加成功");
            responseData.setData(billCode);
        }
        return responseData;
    }

    /**
     * 批发销售工费管理
     * @return
     */
    @GetMapping(value = "/queryProCode")
    @ApiOperation(value = "查询产品编码",notes = "查询对应的产品编码信息",httpMethod = "get", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResponseData queryProCode(@RequestParam(defaultValue = "1") int currentPage, @RequestParam(defaultValue = "3")int pageSize){
        PaginationResult billCodeList = productCodeService.queryProCode(currentPage,pageSize);
        if(billCodeList != null){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("查询成功");
            responseData.setData(billCodeList);
        }
        return responseData;
    }
}
